#include "LeftLoginWidget.h"
#include <QApplication>

namespace UserManagementSystem {
LeftLoginWidget::LeftLoginWidget(QWidget *parent)
    : FramelessWidget {parent}
{
    this->setWindowTitle(QObject::tr("这是左侧窗口"));
    //主布局
    m_mainLayout=new QVBoxLayout(this);


    ///label
    m_titleLabel=new QLabel(QObject::tr("Cameo Oral Scanning"));
    m_infoLabel=new QLabel(tr("Welcome to \nCameo Oral Scanning"));

    //两个输入框的布局
    m_accountLayout=new QVBoxLayout();

    //两个输入框
    m_accountEdit=new EditBox();
    m_passwordEdit=new EditBox(nullptr,true);
    m_passwordEdit->GetInputBox()->setEchoMode(QLineEdit::Password);
    LoadIcon();

    //记住密码
    m_remPassword=new QCheckBox();
    m_remPassword->setText(tr("Remember Password"));

    //登录按钮
    m_loginBtn=new QPushButton(QObject::tr("Log in"),this);

    //忘记密码按钮和注册按钮的布局
    m_forgetAndRigLayout=new QHBoxLayout();
    m_forgetPassBtn=new QPushButton();
    m_vLine=new QFrame();
    m_registerBtn=new QPushButton();

    m_forgetPassBtn->setText(tr("Forget Password "));
    m_registerBtn->setText(tr("Register"));

    //用户协议打开按钮
    m_userTip=new QPushButton();
    m_userTip->setText(tr("* By clicking on login,you agree to the terms of service andprivacy policy."));

    //调用默认设置布局函数
    this->SetDefaultLayout();

    //绑定槽函数
    connect(m_loginBtn,&QPushButton::clicked,this,&LeftLoginWidget::On_LoginBtn_Clicked);
    connect(m_forgetPassBtn,&QPushButton::clicked,this,&LeftLoginWidget::On_ForgetBtn_Clicked);
    connect(m_registerBtn,&QPushButton::clicked,this,&LeftLoginWidget::On_RegisterBtn_Clicked);
    connect(m_userTip,&QPushButton::clicked,this,&LeftLoginWidget::On_UserInfoBtn_Clicked);

}

void LeftLoginWidget::LoadIcon()
{
    //邮箱图标
    QString envelop_path=QApplication::applicationDirPath()+"/res/Login/envelop.png";
    //密钥图标
    QString password_path=QApplication::applicationDirPath()+"/res/Login/unlock.png";

    //显示图标
    QString show_icon=QApplication::applicationDirPath()+"/res/Login/show_eye.png";
    QString noshow_icon=QApplication::applicationDirPath()+"/res/Login/hide_eye.png";;

    //不显示图标
    m_accountEdit->ReadLeftIcon(envelop_path,m_accountEdit->GetLeftIconPointer());


    m_passwordEdit->ReadLeftIcon(password_path,m_passwordEdit->GetLeftIconPointer());
    m_passwordEdit->ReadLeftIcon(show_icon,m_passwordEdit->GetShowIconPointer());
    m_passwordEdit->ReadLeftIcon(noshow_icon,m_passwordEdit->GetNoShowIconPointer());

    m_accountEdit->InitDefaultLayout();
    m_passwordEdit->InitDefaultLayout();

}

void LeftLoginWidget::SetDefaultLayout()
{
    //mainframe右边设置间距//左上右下
    m_mainLayout->setContentsMargins(70,50,70,50);

    //m_accountlayout
    m_accountLayout->addWidget(m_accountEdit);
    m_accountLayout->addSpacing(15);
    m_accountLayout->addWidget(m_passwordEdit);

    //忘记密码按钮和注册按钮
    m_forgetAndRigLayout->setAlignment(Qt::AlignRight);
    m_forgetAndRigLayout->addWidget(m_forgetPassBtn);
    m_forgetAndRigLayout->addWidget(m_vLine);
    m_forgetAndRigLayout->addWidget(m_registerBtn);


    //添加label
    m_mainLayout->setAlignment(Qt::AlignTop);
    m_mainLayout->addWidget(m_titleLabel);
    m_mainLayout->addSpacing(10);
    m_mainLayout->addWidget(m_infoLabel);
    m_mainLayout->addSpacing(10);

    m_mainLayout->addLayout(m_accountLayout);
    m_mainLayout->addWidget(m_remPassword);


    m_mainLayout->addWidget(m_loginBtn);

    //添加忘记密码和注册的布局
    m_mainLayout->addLayout(m_forgetAndRigLayout);

    m_mainLayout->insertStretch(8,1);
    m_mainLayout->addWidget(m_userTip);

    //修改样式
    SetDefaultStyle();

}

void LeftLoginWidget::SetDefaultStyle()
{


    //label的样式
    m_titleLabel->setStyleSheet("font-size:16px;");
    m_infoLabel->setStyleSheet("font-size:21px;font-weight:bold;");




    m_loginBtn->setFixedHeight(40);

    m_loginBtn->setStyleSheet(R"(
    QPushButton {
        background-color: #0078D7;
        border-radius: 20px;
        color: white;
        border: none;

        padding: 8px 16px;
        font-size: 14px;
    }

    QPushButton:hover {
        background-color: #005BB5;
    }

    QPushButton:pressed {
        background-color: #003E8A;
    }
)");


    //忘记密码
    m_vLine->setFrameShape(QFrame::VLine);
    m_vLine->setLineWidth(1);

    QString remaining_btn_style=R"(
    QPushButton {
        background-color: transparent;
        border: none;
    }
    QPushButton:checked {
        background-color: transparent;
    }
    QPushButton:pressed {
        background-color: transparent;
    }
    QPushButton:focus {
        outline: none;
    }
)";

    //这几个按钮的样式
    m_remPassword->setStyleSheet(remaining_btn_style);
    m_forgetPassBtn->setStyleSheet(remaining_btn_style);

    m_registerBtn->setStyleSheet(remaining_btn_style);

    m_userTip->setStyleSheet(remaining_btn_style);

}

void LeftLoginWidget::On_LoginBtn_Clicked()
{
    qDebug()<<"点击登录按钮";
}

void LeftLoginWidget::On_ForgetBtn_Clicked()
{
    qDebug()<<"点击忘记密码按钮";
}

void LeftLoginWidget::On_RegisterBtn_Clicked()
{
    qDebug()<<"点击注册账号按钮";
}

void LeftLoginWidget::On_UserInfoBtn_Clicked()
{
    qDebug()<<"点击用户须知信息按钮";
}
}

